package com.zzusedating.androidapp.fragments;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.reflect.TypeToken;
import com.zzusedating.androidapp.R;
import com.zzusedating.androidapp.adapters.BBSAdapter;
import com.zzusedating.androidapp.model.BBSMessage;
import com.zzusedating.androidapp.tools.BasicCallback;
import com.zzusedating.androidapp.tools.BasicHandlerCallback;
import com.zzusedating.androidapp.tools.ConstantsTools;
import com.zzusedating.androidapp.tools.MyApplication;

import org.json.JSONArray;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Request;
import okhttp3.Response;

public class BBSFragment extends Fragment {

    private Handler handler;
    private Callback callback;

    private RecyclerView recyclerView;
    private ArrayList<BBSMessage> messages;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_bbs_layout, container, false);
        recyclerView = view.findViewById(R.id.FragBBS_Recycler);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
        messages = new ArrayList<>();
        recyclerView.setAdapter(new BBSAdapter(getContext(), messages));
        initCallback();
        MyApplication.getClient().newCall(
                new Request.Builder()
                        .url(String.format(ConstantsTools.GET_MESSAGE_LEFT, 20))
                        .get()
                        .build()
        ).enqueue(callback);
        return view;
    }

    private void initCallback() {
        handler = new Handler(new BasicHandlerCallback() {
            @Override
            public boolean handleMessage(Message msg) {
                switch (msg.what) {
                    case 1:
                        Objects.requireNonNull(recyclerView.getAdapter()).notifyDataSetChanged();
                        break;
                    case -1:
                        MyApplication.Toast("未登录");
                        break;
                    case 0:
//                        MyApplication.Toast("未找到数据");
                        break;
                }
                return true;
            }
        });
        callback = new BasicCallback(handler) {
            @Override
            public void onResponse(Call call, @NonNull Response response) {
                JSONArray array = (JSONArray) getObject(response);
                if (array == null) {
                    handler.sendMessage(ConstantsTools.createMessage(getCode()));
                    return;
                }
                List<BBSMessage> temp = new Gson().fromJson(array.toString(), new TypeToken<List<BBSMessage>>() {
                }.getType());
                messages.clear();
                messages.addAll(temp);
                handler.sendMessage(ConstantsTools.createMessage(1));
            }
        };
    }
}
